<?php
/**
 * Created by IntelliJ IDEA.
 * User: sunminqiang
 * Date: 17/3/3
 * Time: 下午3:15
 */
namespace app\admin\controller;
use app\admin\model\ContactModel;
use think\Db;

class Contact extends Base
{
    public function index()
    {

        $key = input('key');
        $map = [];
        if ($key && $key !== "") {
            $map['title'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page') : 1;
        $limits = 10;// 获取总条数
        $count = Db::name('contact')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $contact= new ContactModel();
        $lists = $contact->getContactByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if (input('get.page')) {
            return json($lists);
        }
        return $this->fetch();
    }

    public function edit_contact()
    {
        $contact= new ContactModel();

        if(request()->isAjax()){

            $param = input('post.');
            $flag = $contact->updateContact($param);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $this->assign('contact',$contact->getOneContact($id));
        return $this->fetch();
    }




    public function warning()
    {
        $key = input('key');
        $map = [];
        if ($key && $key !== "") {
            $map['title'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page') : 1;
        $limits = 10;// 获取总条数
        $count = Db::name('warning')->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $warning = new ContactModel();
        $lists = $warning->getWarningByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if (input('get.page')) {
            return json($lists);
        }
        return $this->fetch();
    }


        public function edit_warning()
    {
        $warning= new ContactModel();

        if(request()->isajax()){
            $param = input('post.');
            $res = array(
                'id'=>$param['id'],
                'language'=>$param['language']
            );

            $flag = $warning->updateWarning($res);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $this->assign('warning',$warning->getOneWarning($id));
        return $this->fetch();
    }




    public function about()
    {

        $key = input('key');
        $map = [];
        if ($key && $key !== "") {
            $map['title'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page') : 1;
        $limits = 10;// 获取总条数
        $count = Db::name('about')->where($map)->where('id','=',1)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $about= new ContactModel();
        $lists = $about->getAboutByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if (input('get.page')) {
            return json($lists);
        }
        return $this->fetch();
    }

    public function edit_about()
    {
        $about= new ContactModel();

        if(request()->isajax()){
            $param = input('post.');
            $res = array(
              'id'=>$param['id'],
              'introduce'=>$param['introduce'],
              'pic'=>$param['pic'],
              'phone'=>$param['phone'],
              'mail'=>$param['mail'],
            );

            $flag = $about->updateAbout($res);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $this->assign('about',$about->getOneAbout($id));
        return $this->fetch();
    }
    public function pic()
    {

        $key = input('key');
        $map = [];
        if ($key && $key !== "") {
            $map['id'] = ['like', "%" . $key . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page') : 1;
        $limits = 10;// 获取总条数
        $count = Db::name('about')->where($map)->where('id','=',2)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $pic= new ContactModel();
        $lists = $pic->getPicByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count);
        $this->assign('val', $key);
        if (input('get.page')) {
            return json($lists);
        }
        return $this->fetch();
    }

    public function edit_pic()
    {
        $pic= new ContactModel();

        if(request()->isajax()){
            $param = input('post.');
            $res = array(
                'id'=>$param['id'],
                'pic'=>$param['pic'],
            );

            $flag = $pic->updatePic($res);
            return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
        }
        $id = input('param.id');
        $this->assign('pic',$pic->getOnePic($id));
        return $this->fetch();
    }




}